<html>
<head><meta charset="utf-8"><title>MaybeResult · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/MaybeResult.html">MaybeResult</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="152470033"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/MaybeResult/near/152470033" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daan de Graaf <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/MaybeResult.html#152470033">(Dec 24 2018 at 13:35)</a>:</h4>
<p>As a new contributor to rustc, I got a bit confused about the MaybeResult trait. I have a variable where the only type bound is MaybeResult&lt;...&gt;, and I would like my function to return an Option&lt;..&gt; based on the value of the MaybeResult variable I have. I have not encountered this trait before so I am not sure how to work with it. </p>
<p>Is possible to turn a MaybeResult&lt;T&gt; into an Option&lt;T&gt;? Or would I be better off changing the function signature to return a MaybeResult&lt;..&gt; as well? </p>
<p>I am also kind of curious what the design rationale is behind the trait itself.</p>



<a name="152470725"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/MaybeResult/near/152470725" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/MaybeResult.html#152470725">(Dec 24 2018 at 13:58)</a>:</h4>
<p>You can find the rustdoc for <code>MaybeResult</code> <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc/ty/layout/trait.MaybeResult.html" target="_blank" title="https://doc.rust-lang.org/nightly/nightly-rustc/rustc/ty/layout/trait.MaybeResult.html">here</a>. From the implementors it has it appears that this is either just <code>T</code> or <code>Result&lt;T, E&gt;</code>.</p>



<a name="152470739"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/MaybeResult/near/152470739" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/MaybeResult.html#152470739">(Dec 24 2018 at 13:59)</a>:</h4>
<p>So yes, you should be able to convert a <code>T: MaybeResult&lt;...&gt;</code> to an <code>Option</code> fairly easily.</p>



<a name="152470808"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/MaybeResult/near/152470808" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/MaybeResult.html#152470808">(Dec 24 2018 at 14:01)</a>:</h4>
<p>There two ways to approach this, actually</p>



<a name="152470810"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/MaybeResult/near/152470810" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/MaybeResult.html#152470810">(Dec 24 2018 at 14:01)</a>:</h4>
<p>one is to simply add a method to the trait that does exactly the conversion you want.</p>



<a name="152470848"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/MaybeResult/near/152470848" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/MaybeResult.html#152470848">(Dec 24 2018 at 14:02)</a>:</h4>
<p>the other would be something along the lines of</p>



<a name="152470867"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/MaybeResult/near/152470867" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/MaybeResult.html#152470867">(Dec 24 2018 at 14:04)</a>:</h4>
<div class="codehilite"><pre><span></span><span class="k">fn</span><span class="o">&lt;</span><span class="n">V</span><span class="p">,</span><span class="w"> </span><span class="n">T</span>: <span class="nc">MaybeResult</span><span class="o">&lt;</span><span class="n">V</span><span class="o">&gt;</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nb">Into</span><span class="o">&lt;</span><span class="nb">Option</span><span class="o">&lt;</span><span class="n">V</span><span class="o">&gt;&gt;&gt;&gt;</span><span class="p">(...)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="p">...</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
</pre></div>



<a name="152470932"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/MaybeResult/near/152470932" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/MaybeResult.html#152470932">(Dec 24 2018 at 14:04)</a>:</h4>
<p>changing <code>Into</code> into your trait in this case, because there is no such implementation like <code>Result&lt;T, E&gt;: Into&lt;Option&lt;T&gt;&gt;</code>.</p>



<a name="152471074"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/MaybeResult/near/152471074" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daan de Graaf <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/MaybeResult.html#152471074">(Dec 24 2018 at 14:09)</a>:</h4>
<p>Thanks a lot <span class="user-mention" data-user-id="123586">@nagisa</span>! I think I should be able to get both those options working, I'll try the second option for now just to not mess too much with the trait itself</p>



<a name="152471117"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/MaybeResult/near/152471117" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/MaybeResult.html#152471117">(Dec 24 2018 at 14:10)</a>:</h4>
<p>I think changing the trait itself is a better and cleaner option overall</p>



<a name="152471344"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/MaybeResult/near/152471344" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daan de Graaf <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/MaybeResult.html#152471344">(Dec 24 2018 at 14:16)</a>:</h4>
<p>Yes that's true for sure, I just do not feel very confident in changing the trait without really knowing how it's normally used <span class="emoji emoji-1f605" title="sweat smile">:sweat_smile:</span>. For example, I have no idea how an implementation can pull a <code>T</code> from a <code>MaybeResult&lt;T&gt;</code> without a method like <code>into_option()</code> or <code>unwrap</code> available. Then again, maybe that should not stop me from experimenting</p>



<a name="152472053"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/MaybeResult/near/152472053" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daan de Graaf <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/MaybeResult.html#152472053">(Dec 24 2018 at 14:37)</a>:</h4>
<p>I ended up just adding an <code>ok()</code> method to the trait, corresponding to the <code>ok()</code> on normal <code>Result&lt;T&gt;</code>. It is indeed a much cleaner solution, and I think it makes sense given that the trait is so closely related to <code>Result</code>. <span class="emoji emoji-263a" title="smile">:smile:</span></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>